package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.RoleDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.exception.BusinessRuntimeException;
import com.itheima.pojo.Menu;
import com.itheima.pojo.Permission;
import com.itheima.pojo.Role;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * Author  赵晴
 * Date 2020/07/08  16:45
 * Version 1.0
 */
@Service
@Slf4j
public class RoleServiceImpl implements RoleService {
    @Autowired
    RoleDao roleDao;

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        Page<Role> page = roleDao.findByCondition(queryPageBean.getQueryString());
        return new PageResult(page.getTotal(), page);
    }

    @Override
    public List<Permission> findPermission() {
        return roleDao.findPermission();
    }

    @Override
    public List<Menu> findMenu() {
        return roleDao.findMenu();
    }

    @Override
    public Role findByNameAndKeyword(Role role) {
        return roleDao.findByNameAndKeyword(role);
    }

    //添加角色 同时添加和用户 权限 菜单 的关联
    @Override
    public void add(Role role, Integer[] permissionIds, Integer[] menuIds) {
        roleDao.add(role);
        setRelation(role.getId(), permissionIds, menuIds);
    }


    @Override
    public Role findById(Integer id) {
        return roleDao.findById(id);
    }

    @Override
    public List<Integer> findPermissionIdsByRoleId(Integer roleId) {
        return roleDao.findPermissionIdsByRoleId(roleId);
    }

    @Override
    public List<Integer> findMenuIdsByRoleId(Integer roleId) {
        return roleDao.findMenuIdsByRoleId(roleId);
    }


    @Override
    public void edit(Role role, Integer[] permissionIds, Integer[] menuIds) {
        List<Integer> userIdsByRoleId = roleDao.findUserIdsByRoleId(role.getId());
        if (userIdsByRoleId.size()>0){
            throw new BusinessRuntimeException("该角色正在被用户使用,无法修改");
        }
        //删除旧的关联
        roleDao.delRelationPermission(role.getId());
        roleDao.delRelationMenu(role.getId());
        setRelation(role.getId(), permissionIds, menuIds);
        roleDao.edit(role);
    }


    @Override
    public void delById(Integer id) {
        List<Integer> userIdsByRoleId = roleDao.findUserIdsByRoleId(id);
        if (userIdsByRoleId.size()>0){
            throw new BusinessRuntimeException("该角色正在被使用,无法删除");
        }
        roleDao.delRelationMenu(id);
        roleDao.delRelationPermission(id);
        roleDao.delById(id);
    }

    //设置角色和 权限 菜单 用户 的关联关系
    private void setRelation(Integer roleId, Integer[] permissionIds, Integer[] menuIds) {

        if (permissionIds != null && permissionIds.length > 0) {
            for (Integer permissionId : permissionIds) {
                roleDao.setRelationPermission ( roleId, permissionId );
            }
        }

        if (menuIds != null && menuIds.length > 0) {
            for (Integer menuId : menuIds) {
                roleDao.setRelationMenu ( roleId, menuId );
            }
        }
    }
    //查询所有角色
    @Override
    public List<Role> findAll() {
        return roleDao.findAll();
    }
}
